package Model;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import map.CellStatus.CellStatusType;
import map.Map;

public class Car {
	private Map map;
	private Point position;
	private Point source;
	private Point destination;
	private String color;
	private String[] colors = {"black", "blu", "green", "gray"};
	private List<Point> pathToFollow;
	private HashMap<Point, CellStatusType> actionForPoint = new HashMap<Point, CellStatusType>();

	public List<Point> getPathToFollow() {
		return pathToFollow;
	}

	public Car(Point startPosition, Point finalPosition, Map map) {
		this.position = startPosition;
		this.source = new Point(startPosition);
		this.destination = new Point(finalPosition);
		Random generator = new Random();
		int c = generator.nextInt(4);
		this.color = colors[c];
		this.pathToFollow = new ArrayList<Point>(map.getPointsListFrom(startPosition, finalPosition, actionForPoint));

		this.map = map;
	}
	
	public String getColor(){
		return color;
	}

	public Point getPosition() {
		return position;
	}

	public Point getSource() {
		return source;
	}

	public Point getDestination() {
		return destination;
	}

	
	public void updatePosition(){
		if(pathToFollow.isEmpty()){
			map.getMap()[this.position.x][this.position.y].setOccupied(false);
			return;
		}
		Point curr = pathToFollow.get(0);
		if(map.getMap()[curr.x][curr.y].isOccupied())
			return;
		map.getMap()[curr.x][curr.y].setOccupied(true);
		map.getMap()[this.position.x][this.position.y].setOccupied(false);
		this.position.x = curr.x;
		this.position.y = curr.y;
//		System.out.println(position);
		//AGGIORNAMENTO DEL GRAFO DEL TRAFFICO 
		
		pathToFollow.remove(0);
	
		
	}

	public boolean checkFinish() {
		if(destination.equals(position) || pathToFollow.isEmpty()){
			map.getMap()[this.position.x][this.position.y].setOccupied(false);
			return true;
		}
		return false;
	}
	
	public CellStatusType getFromHashMap(){
		return actionForPoint.get(position);
	}

	public void updatePathTofollow() {
		this.pathToFollow = new ArrayList<Point>();
		this.actionForPoint = new HashMap<Point, CellStatusType>();
//		System.out.println("change " + source +" - " + position + " to " + destination);
		source = position;
		this.pathToFollow = new ArrayList<Point>(map.getPointsListFrom(position, destination, actionForPoint));
//		System.out.println("----------------@@@@@@@@@@@@@@@@@@@@@");
	}

}
